

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cdt.data.acyclic_graph_generator &mdash; Causal Discovery Toolbox 0.5.22 documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.png"/>
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"extensions": ["tex2jax.js"], "jax": ["input/TeX", "output/HTML-CSS"], "tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "displayMath": [["$$", "$$"], ["\\[", "\\]"]], "processEscapes": true}, "HTML-CSS": {"fonts": ["TeX"]}})</script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/banner.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.5.22
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../index.html">Causal Discovery Toolbox Documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial.html">Get started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../causality.html">cdt.causality</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../independence.html">cdt.independence</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../data.html">cdt.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils.html">cdt.utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../metrics.html">cdt.metrics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../settings.html">Toolbox Settings</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models.html">PyTorch Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../developer.html">Developer Documentation</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Causal Discovery Toolbox</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>cdt.data.acyclic_graph_generator</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cdt.data.acyclic_graph_generator</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Acyclic Graph Generator.</span>

<span class="sd">Generates a dataset out of an acyclic FCM.</span>
<span class="sd">Author : Olivier Goudet and Diviyan Kalainathan</span>

<span class="sd">.. MIT License</span>
<span class="sd">..</span>
<span class="sd">.. Copyright (c) 2018 Diviyan Kalainathan</span>
<span class="sd">..</span>
<span class="sd">.. Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="sd">.. of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="sd">.. in the Software without restriction, including without limitation the rights</span>
<span class="sd">.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="sd">.. copies of the Software, and to permit persons to whom the Software is</span>
<span class="sd">.. furnished to do so, subject to the following conditions:</span>
<span class="sd">..</span>
<span class="sd">.. The above copyright notice and this permission notice shall be included in all</span>
<span class="sd">.. copies or substantial portions of the Software.</span>
<span class="sd">..</span>
<span class="sd">.. THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="sd">.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="sd">.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="sd">.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="sd">.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="sd">.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="sd">.. SOFTWARE.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">scale</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">from</span> <span class="nn">.causal_mechanisms</span> <span class="kn">import</span> <span class="p">(</span><span class="n">LinearMechanism</span><span class="p">,</span>
                                <span class="n">Polynomial_Mechanism</span><span class="p">,</span>
                                <span class="n">SigmoidAM_Mechanism</span><span class="p">,</span>
                                <span class="n">SigmoidMix_Mechanism</span><span class="p">,</span>
                                <span class="n">GaussianProcessAdd_Mechanism</span><span class="p">,</span>
                                <span class="n">GaussianProcessMix_Mechanism</span><span class="p">,</span>
                                <span class="n">NN_Mechanism</span><span class="p">,</span>
                                <span class="n">gmm_cause</span><span class="p">,</span> <span class="n">normal_noise</span><span class="p">,</span> <span class="n">uniform_noise</span><span class="p">)</span>


<div class="viewcode-block" id="AcyclicGraphGenerator"><a class="viewcode-back" href="../../../data.html#cdt.data.AcyclicGraphGenerator">[docs]</a><span class="k">class</span> <span class="nc">AcyclicGraphGenerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generate an acyclic graph and data given a causal mechanism.</span>

<span class="sd">    Args:</span>
<span class="sd">        causal_mechanism (str): currently implemented mechanisms:</span>
<span class="sd">            [&#39;linear&#39;, &#39;polynomial&#39;, &#39;sigmoid_add&#39;,</span>
<span class="sd">            &#39;sigmoid_mix&#39;, &#39;gp_add&#39;, &#39;gp_mix&#39;, &#39;nn&#39;].</span>
<span class="sd">        noise (str or function): type of noise to use in the generative process</span>
<span class="sd">            (&#39;gaussian&#39;, &#39;uniform&#39; or a custom noise function).</span>
<span class="sd">        noise_coeff (float): Proportion of noise in the mechanisms.</span>
<span class="sd">        initial_variable_generator (function): Function used to init variables</span>
<span class="sd">            of the graph, defaults to a Gaussian Mixture model.</span>
<span class="sd">        npoints (int): Number of data points to generate.</span>
<span class="sd">        nodes (int): Number of nodes in the graph to generate.</span>
<span class="sd">        parents_max (int): Maximum number of parents of a node.</span>
<span class="sd">        expected_degree (int): Degree (number of edge per node) expected,</span>
<span class="sd">            only used for erdos graph</span>
<span class="sd">        dag_type (str): type of graph to generate (&#39;default&#39;, &#39;erdos&#39;)</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import AcyclicGraphGenerator</span>
<span class="sd">        &gt;&gt;&gt; generator = AcyclicGraphGenerator(&#39;linear&#39;, npoints=1000)</span>
<span class="sd">        &gt;&gt;&gt; data, graph = generator.generate()</span>
<span class="sd">        &gt;&gt;&gt; generator.to_csv(&#39;generated_graph&#39;)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">causal_mechanism</span><span class="p">,</span> <span class="n">noise</span><span class="o">=</span><span class="s1">&#39;gaussian&#39;</span><span class="p">,</span>
                 <span class="n">noise_coeff</span><span class="o">=.</span><span class="mi">4</span><span class="p">,</span>
                 <span class="n">initial_variable_generator</span><span class="o">=</span><span class="n">gmm_cause</span><span class="p">,</span>
                 <span class="n">npoints</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">nodes</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">parents_max</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">expected_degree</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                 <span class="n">dag_type</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AcyclicGraphGenerator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mechanism</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;linear&#39;</span><span class="p">:</span> <span class="n">LinearMechanism</span><span class="p">,</span>
                          <span class="s1">&#39;polynomial&#39;</span><span class="p">:</span> <span class="n">Polynomial_Mechanism</span><span class="p">,</span>
                          <span class="s1">&#39;sigmoid_add&#39;</span><span class="p">:</span> <span class="n">SigmoidAM_Mechanism</span><span class="p">,</span>
                          <span class="s1">&#39;sigmoid_mix&#39;</span><span class="p">:</span> <span class="n">SigmoidMix_Mechanism</span><span class="p">,</span>
                          <span class="s1">&#39;gp_add&#39;</span><span class="p">:</span> <span class="n">GaussianProcessAdd_Mechanism</span><span class="p">,</span>
                          <span class="s1">&#39;gp_mix&#39;</span><span class="p">:</span> <span class="n">GaussianProcessMix_Mechanism</span><span class="p">,</span>
                          <span class="s1">&#39;nn&#39;</span><span class="p">:</span> <span class="n">NN_Mechanism</span><span class="p">}[</span><span class="n">causal_mechanism</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;V</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nodes</span><span class="p">)])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span> <span class="o">=</span> <span class="n">nodes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">npoints</span> <span class="o">=</span> <span class="n">npoints</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">noise</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;gaussian&#39;</span><span class="p">:</span> <span class="n">normal_noise</span><span class="p">,</span>
                          <span class="s1">&#39;uniform&#39;</span><span class="p">:</span> <span class="n">uniform_noise</span><span class="p">}[</span><span class="n">noise</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">noise</span> <span class="o">=</span> <span class="n">noise</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">noise_coeff</span> <span class="o">=</span> <span class="n">noise_coeff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">nodes</span><span class="p">,</span> <span class="n">nodes</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parents_max</span> <span class="o">=</span> <span class="n">parents_max</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">expected_degree</span> <span class="o">=</span> <span class="n">expected_degree</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dag_type</span> <span class="o">=</span> <span class="n">dag_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_generator</span> <span class="o">=</span> <span class="n">initial_variable_generator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cfunctions</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">g</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="AcyclicGraphGenerator.init_dag"><a class="viewcode-back" href="../../../data.html#cdt.data.AcyclicGraphGenerator.init_dag">[docs]</a>    <span class="k">def</span> <span class="nf">init_dag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">verbose</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Redefine the structure of the graph depending on dag_type</span>
<span class="sd">        (&#39;default&#39;, &#39;erdos&#39;)</span>

<span class="sd">        Args:</span>
<span class="sd">            verbose (bool): Verbosity</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dag_type</span> <span class="o">==</span> <span class="s1">&#39;default&#39;</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">):</span>
                <span class="n">nb_parents</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">parents_max</span><span class="p">,</span> <span class="n">j</span><span class="p">])</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">),</span> <span class="n">nb_parents</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">dag_type</span> <span class="o">==</span> <span class="s1">&#39;erdos&#39;</span><span class="p">:</span>
            <span class="n">nb_edges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">expected_degree</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span>
            <span class="n">prob_connection</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">nb_edges</span><span class="o">/</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">)</span>
            <span class="n">causal_order</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">permutation</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">))</span>

            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">node</span> <span class="o">=</span> <span class="n">causal_order</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="n">possible_parents</span> <span class="o">=</span> <span class="n">causal_order</span><span class="p">[(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">):]</span>
                <span class="n">num_parents</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span> <span class="o">-</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span>
                                                 <span class="n">p</span><span class="o">=</span><span class="n">prob_connection</span><span class="p">)</span>
                <span class="n">parents</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">possible_parents</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">num_parents</span><span class="p">,</span>
                                           <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[</span><span class="n">parents</span><span class="p">,</span> <span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">g</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">)</span>
            <span class="k">assert</span> <span class="ow">not</span> <span class="nb">list</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">simple_cycles</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">g</span><span class="p">))</span>

        <span class="k">except</span> <span class="ne">AssertionError</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Regenerating, graph non valid...&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_dag</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span></div>


<div class="viewcode-block" id="AcyclicGraphGenerator.init_variables"><a class="viewcode-back" href="../../../data.html#cdt.data.AcyclicGraphGenerator.init_variables">[docs]</a>    <span class="k">def</span> <span class="nf">init_variables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Redefine the causes, mechanisms and the structure of the graph,</span>
<span class="sd">        called by ``self.generate()`` if never called.</span>

<span class="sd">        Args:</span>
<span class="sd">            verbose (bool): Verbosity</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_dag</span><span class="p">(</span><span class="n">verbose</span><span class="p">)</span>

        <span class="c1"># Mechanisms</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cfunctions</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">mechanism</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[:,</span> <span class="n">i</span><span class="p">])),</span>
                                          <span class="bp">self</span><span class="o">.</span><span class="n">npoints</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">noise</span><span class="p">,</span> <span class="n">noise_coeff</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">noise_coeff</span><span class="p">)</span>
                           <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[:,</span> <span class="n">i</span><span class="p">])</span>
                           <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_generator</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">)]</span></div>

<div class="viewcode-block" id="AcyclicGraphGenerator.generate"><a class="viewcode-back" href="../../../data.html#cdt.data.AcyclicGraphGenerator.generate">[docs]</a>    <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rescale</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate data from an FCM defined in ``self.init_variables()``.</span>

<span class="sd">        Args:</span>
<span class="sd">            rescale (bool): rescale the generated data (recommended)</span>

<span class="sd">        Returns:</span>
<span class="sd">            tuple: (pandas.DataFrame, networkx.DiGraph), respectively the</span>
<span class="sd">            generated data and graph.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfunctions</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_variables</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">nx</span><span class="o">.</span><span class="n">topological_sort</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">g</span><span class="p">):</span>
            <span class="c1"># Root cause</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;V</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfunctions</span><span class="p">[</span><span class="n">i</span><span class="p">](</span><span class="bp">self</span><span class="o">.</span><span class="n">npoints</span><span class="p">)</span>
            <span class="c1"># Generating causes</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;V</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfunctions</span><span class="p">[</span><span class="n">i</span><span class="p">](</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">nonzero</span><span class="p">()[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">rescale</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;V</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span> <span class="o">=</span> <span class="n">scale</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;V</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">g</span></div>

<div class="viewcode-block" id="AcyclicGraphGenerator.to_csv"><a class="viewcode-back" href="../../../data.html#cdt.data.AcyclicGraphGenerator.to_csv">[docs]</a>    <span class="k">def</span> <span class="nf">to_csv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fname_radical</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the generated data to the csv format by default,</span>
<span class="sd">        in two separate files: data, and the adjacency matrix of the</span>
<span class="sd">        corresponding graph.</span>

<span class="sd">        Args:</span>
<span class="sd">            fname_radical (str): radical of the file names. Completed by</span>
<span class="sd">               ``_data.csv`` for the data file and ``_target.csv`` for the</span>
<span class="sd">               adjacency matrix of the generated graph.</span>
<span class="sd">            \**kwargs: Optional keyword arguments can be passed to pandas.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">fname_radical</span><span class="o">+</span><span class="s1">&#39;_data.csv&#39;</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">)</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">fname_radical</span> \
                                                       <span class="o">+</span> <span class="s1">&#39;_target.csv&#39;</span><span class="p">,</span>
                                                       <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Graph has not yet been generated. </span><span class="se">\</span>
<span class="s2">                              Use self.generate() to do so.&quot;</span><span class="p">)</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; Copyright 2018, Diviyan Kalainathan, Olivier Goudet

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>